/**********************************************************************************/
/*This code is for version 2.XXX  needs to be corrected but its still a good  idea*/
/**********************************************************************************/
if exists (select 1 from sys.procedures where name='printer' and schema_id=schema_id('udb'))
	drop procedure udb.printer;

go
create proc udb.printer 
@varchar varchar(max)
,@break_string_flag bit =1
,@print_break_length smallint=1000
as
/*Input Validation*/
if @print_break_length>4000
	raisError('@print_break_length must be less or equal to 4000',16,1);

/*Declaration*/
declare @counter smallint=1;
declare @print_string varchar(4000);
declare @break_string varchar(600);
declare @CRLF_location smallint=0;
declare @looper smallint =1;
while @counter<=len(@varchar)
BEGIN
	set @CRLF_location=@print_break_length;
	set @looper=1;
	while @CRLF_location=@print_break_length 
	BEGIN
	PRINT 'HERE'
		set @print_string = substring(@varchar,@counter,@print_break_length*@looper); 
		set @CRLF_location=charindex(char(10)+char(13),reverse(@print_string),0)+1;
		set @looper+=1;
	END
	print @CRLF_Location;
	print @looper;
	print @print_break_length;
	set @print_string = substring(@varchar,@counter,@print_break_length*@looper -@CRLF_location); 
	
	print @print_string;

	if @break_string_flag=1
	BEGIN
		set @break_string=char(13)+char(10)
		+'/******************************************************************************/'+char(13)+char(10)
		+'/*                                    udb.print                               */'+char(13)+char(10)
		+'/* len(@varchar):'+convert(char(10),len(@varchar))+replicate(' ',50)+' */'+char(13)+char(10)
		+'/* @counter:'+convert(char(10),@counter)+replicate(' ',55)+' */'+char(13)+char(10)
		+'/* len(@print_string):'+convert(char(10),len(@print_string))+replicate(' ',45)+' */'+char(13)+char(10)
		+'/* @CRLF_location:'+convert(char(10),@CRLF_location)+replicate(' ',49)+' */'+char(13)+char(10)
		+'/******************************************************************************/'+char(13)+char(10)
		print @break_string;
	END

	set @counter+=len(@print_string);

END
go
declare @varchar varchar(max)='';

select
@varchar+=text
from syscomments
where
id=object_id('udb.logInsert')

execute udb.printer @varchar,1,30
